/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_data_analysis.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/1 by jikw                                  */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <redis/redis_core.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/redis/config.h"
#include "frbyte_buffer.h"
#include "apmtest_cunit.h"

typedef struct __st_L7session_redis_private
{
    unsigned long ul_seq;                             /*!< 序列号，1次req和response时，使用同一个序列号 */
    L4_transaction st_l4_trans;                       /*!< 数据 */
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    Frbyte_Packet *p_resp_packet;                     /*!< 存相应bytebuff数据 */
}L7session_redis_private;

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

extern L7session_redis_private* __redis_private_create(void);
extern int __resp_data_analysis(Conntrack *p_st_conn
                                 , Apm_packet *p_st_packet
                                 , Frhash_header *p_st_hash_report
                                 , L7_session *p_st_session);
extern void list_report_node_free(Frlist_node *p_st_node);

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, p_st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_EQUAL(p_st_session, NULL);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("+OK\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 0;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 6;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("+OK\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 5;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 6;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 5);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("+OK\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 5;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 2;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 25;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 0;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 5);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case5(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("+OK\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 5;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 0;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 5);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case6(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("$-1\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 5;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 6;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 5);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case7(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("$14\r\nwww.google.com\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 21;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 6;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 21);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case8(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("$4\r\n\x01\x02\x03\x04\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 10;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 6;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case9(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("*2\r\n$14\r\nwww.google.com\r\n$14\r\nwww.taobao.com\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 46;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 6;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 46);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

static void case10(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    frhash2_create(&st_hash_report, 2);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("*3\r\n$4\r\n\x09\x0a\x0b\x0c\r\n$4\r\n\x05\x06\x07\x08\r\n$-1\r\n");
    if(p_st_packet->p_payload == NULL)
        goto ERROR;
    p_st_packet->ui_payload_len = 29;
    p_st_packet->st_timestamp.tv_sec = 10;
    p_st_packet->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __redis_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 6;
    ((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_packet, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 29);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_redis_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frhash2_free(&st_hash_report, list_report_node_free);
    redis_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            redis_session_destroy(p_st_session->p_session_private);
        frfree(p_st_session);
    }
    return;
}

int add_suite__resp_data_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_data_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL ||
        CU_add_test(pSuite, "case7", case7) == NULL ||
        CU_add_test(pSuite, "case8", case8) == NULL ||
        CU_add_test(pSuite, "case9", case9) == NULL ||
        CU_add_test(pSuite, "case10", case10) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
